<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools :left-show="false">
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addShow('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table row-key="id" style="margin: 10px 0;" :data="permissions">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="addShow(row.id,2)">添加</el-button>
            <el-button type="text" @click="editShow(row)">编辑</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增编辑弹层 -->
    <el-dialog title="权限点" :visible.sync="showDialog" @close="onClose">
      <!-- 表单 -->
      <el-form ref="perForm" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="addPermission">确定</el-button>
          <el-button size="small" @click="onClose">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { transListToTree } from '@/utils'
import { getPermissions, addPermission, editPermission } from '@/api/permission'
export default {
  data() {
    return {
      permissions: [],
      showDialog: false,
      formData: {
        name: '', // 权限点名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '0', // 权限启用状态
        type: '',
        pid: ''
      }
    }
  },

  created() {
    this.loadPermissions()
  },

  methods: {
    async  loadPermissions() {
      const res = await getPermissions()
      this.permissions = transListToTree(res, '0')
    },
    addShow(pid, type) {
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    onClose() {
      this.formData = {
        name: '', // 权限点名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '0', // 权限启用状态
        type: '',
        pid: ''
      }
      this.showDialog = false
    },
    async addPermission() {
      if (this.formData.id) {
        await editPermission(this.formData)
        this.$message.success('修改成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }
      this.onClose()
      this.loadPermissions()
    },
    editShow(row) {
      this.showDialog = true
      this.formData = { ...row }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
